package com.yunfeisoft.business.controller;

import com.yunfeisoft.business.model.Bed;
import com.yunfeisoft.business.model.Room;
import com.yunfeisoft.business.service.inter.BedService;
import com.yunfeisoft.business.service.inter.RoomService;
import com.yunfeisoft.common.base.BaseController;
import com.yunfeisoft.common.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ClassName: RoomController
 * Description: 房间Controller
 * Author: Jackie liu
 * Date: 2020-01-13
 */
@RestController
public class RoomController extends BaseController {

    @Autowired
    private RoomService roomService;
    @Autowired
    private BedService bedService;

    /**
     * 添加房间
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/room/save", method = RequestMethod.POST)
    public Response save(Room record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "floorNum", "楼层为空");
        validator.required(request, "roomNum", "房间号为空");
        validator.required(request, "type", "类型为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        if (CollectionUtils.isEmpty(record.getBedList())) {
            return ResponseUtils.success("床位信息为空");
        }

        record.setId(KeyUtils.getKey());
        Iterator<Bed> iterator = record.getBedList().iterator();
        while (iterator.hasNext()) {
            Bed bed = iterator.next();
            if (bed.getExists() == null) {
                iterator.remove();
                continue;
            }
            bed.setRoomId(record.getId());
        }

        if (CollectionUtils.isEmpty(record.getBedList())) {
            return ResponseUtils.success("床位信息为空");
        }

        roomService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改房间
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/room/modify", method = RequestMethod.POST)
    public Response modify(Room record, HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "id", "参数错误");
        validator.required(request, "floorNum", "楼层为空");
        validator.required(request, "roomNum", "房间号为空");
        validator.required(request, "type", "类型为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        if (CollectionUtils.isEmpty(record.getBedList())) {
            return ResponseUtils.success("床位信息为空");
        }

        Iterator<Bed> iterator = record.getBedList().iterator();
        while (iterator.hasNext()) {
            Bed bed = iterator.next();
            if (bed.getExists() == null) {
                iterator.remove();
                continue;
            }
            bed.setRoomId(record.getId());
        }

        if (CollectionUtils.isEmpty(record.getBedList())) {
            return ResponseUtils.success("床位信息为空");
        }
        roomService.modify(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询房间
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/room/query", method = {RequestMethod.POST, RequestMethod.GET})
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        Room record = roomService.load(id);
        if (record != null) {
            List<Bed> bedList = bedService.queryByRoomId(id);
            record.setBedList(bedList);
        }
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询房间
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/room/list", method = {RequestMethod.POST, RequestMethod.GET})
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String floorNum = ServletRequestUtils.getStringParameter(request, "floorNum", null);
        String roomNum = ServletRequestUtils.getStringParameter(request, "roomNum", null);
        String type = ServletRequestUtils.getStringParameter(request, "type", null);

        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("floorNum", floorNum);
        params.put("roomNum", roomNum);
        params.put("type", type);

        Page<Room> page = roomService.queryPage(params);
        return ResponseUtils.success(page);
    }

    /**
     * 获取所有房间
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/room/listAll", method = {RequestMethod.POST, RequestMethod.GET})
    public Response listAll(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        List<Room> page = roomService.listAll(params);
        return ResponseUtils.success(page);
    }

    @RequestMapping(value = "/web/room/roomList", method = {RequestMethod.POST, RequestMethod.GET})
    public Response roomList(HttpServletRequest request, HttpServletResponse response) {
        String floorNum = ServletRequestUtils.getStringParameter(request, "floorNum", null);

        Map<String, Object> params = new HashMap<String, Object>();
        initParams(params, request);
        params.put("floorNum", floorNum);
        List<Room> roomList = roomService.queryListCondition(params);
        if (StringUtils.isBlank(floorNum)) {
            // 只过滤楼层号
            List<String> list = roomList.stream().map(Room::getFloorNum).distinct().collect(Collectors.toList());
            return ResponseUtils.success(list);
        }
        return ResponseUtils.success(roomList);
    }

    /**
     * 批量删除房间
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/room/delete", method = {RequestMethod.POST, RequestMethod.GET})
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        roomService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
